package com.lanou.zhihu.service.impl;

import com.lanou.zhihu.entity.Comment;
import com.lanou.zhihu.mapper.CommentMapper;
import com.lanou.zhihu.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/10/26.
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public boolean add(Comment comment) {
        Integer result = commentMapper.add(comment);
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean delete(Integer userId, Integer cid) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", userId);
        map.put("cid", cid);
        Comment comment = commentMapper.findByUidCid(map);
        if (comment != null) {
            Integer result = commentMapper.deleteByid(cid);
            if (result == 1) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public List<Comment> findByAid(Integer aid) {
        List<Comment> commentList = commentMapper.findByAid(aid);
        return commentList;
    }

    @Override
    public Integer getCountByAid(Integer aid) {
        Integer count = commentMapper.getCountByAid(aid);
        return count;
    }
}
